
// json降阶处理
export function reduceJson(json, path) {
  for (let childKey in json) {
    let child = json[childKey];
    if (typeof child === "object") {
      let parsed = this.reduceJson(child, path + "." + childKey); //解析此节点
      // 将此节点解析后的内容添加到当前根节点，删除原有节点
      for (let parsedKey in parsed) {
        // let attribute = { //非object已经转换，此处直接添加
        //     "value": parsed[parsedKey],
        //     "path": path+"."+parsedKey
        // }
        eval("json." + parsedKey + " = parsed[parsedKey]"); //初始化节点
      }
      delete json[childKey];
    } else {
      let attribute = {
        value: child,
        path: path + "." + childKey
      };
      json[childKey] = attribute;
    }
  }
  return json;
}

// json升阶处理
export function ascendJson(json) {
  let ascendedJson = {};
  for (let childKey in json) {
    let child = json[childKey];
    let key = "";
    if (typeof child.path != "undefined") {
      let paths = child.path.split(".");
      paths.shift(); //移除第一个root
      paths.map(path => {
        key += "." + path;
        if (eval("typeof ascendedJson" + key) === "undefined") {
          eval("ascendedJson" + key + "= {}");
        }
      });
      eval("ascendedJson" + key + "= child.value");
    }
  }
  return ascendedJson;
}